<?php
require_once 'BaseController.php';
require_once APPLICATION_PATH.'/models/Navbar.php';

class NavbarController extends BaseController{

    public function navbarAction(){
        $tblnav = new Navbar();
        $this->view->navbar = $tblnav->getNavbar();
    }

    public function editnavAction(){
        $id = $this->getCheckedId();
        $tblnav = new Navbar();
        $this->view->nav = $tblnav->getNavById($id);
        $this->view->title = '修改导航项';
        if($this->view->nav == null){
            //没有找到
            header('Location:'.ADMIN_PATH.'/Navbar/navbar');
            exit;
        }
        $this->view->act = 'edit';
    }

    public function addnavAction(){
        $this->view->nav = Array(
            'id' => '',
            'title' => '',
            'order' => '',
            'link' => '',
        );
        $this->view->act = 'add';
        $this->view->title = '添加导航项';
        $this->render('editnav');
    }

    public function delnavAction(){
        $id = $this->getCheckedId();
        $tblnav = new Navbar();
        if($tblnav->deleteNav($id)<1){
            //删除失败
        }else {
            //删除成功
        }
        header('Location:'.ADMIN_PATH.'/Navbar/navbar');
        exit;
    }

    public function doupdateAction(){
        $act = $this->_getParam('act');
        $tblnav = new Navbar();
        $set = Array(
            'title' => $this->_getParam('title'),
            'order' => $this->_getParam('order'),
            'link' => $this->_getParam('link'),
        );

        if($act == 'edit'){
            $id = $this->getCheckedId();
            $affected = $tblnav->updateNav($id, $set);
            if($affected == 0){
                echo '更新无效：您没有对内容进行任何更改！';
            }else if($affected == 1){
                echo '更新成功！';
            }else{
                echo '更新失败！请认真检查您所填写的内容！';
            }
        }
        else if($act == 'add'){
            if($tblnav->addNav($set)<1){
                //添加失败
            }else {
                //添加成功
            }
        }
        else{}
        header('Location:'.ADMIN_PATH.'/Navbar/navbar');
        exit;
    }

    private function getCheckedId(){
        $id = $this->_getParam('id');
        if(!is_numeric($id)){
            //参数不合法
            header('Location:'.ADMIN_PATH.'/Navbar/navbar');
            exit;
        }
        return $id;
    }
}
